package com.minelx.datamicroscope.core.condition;

import com.alibaba.fastjson.JSONObject;
import com.minelx.datamicroscope.core.values.CommonField;
import com.minelx.sqlstructures.core.ColumnType;
import com.minelx.sqlstructures.statements.clause.Sql;

public class OperatorOperand implements Operand {

	private final String value;

	private final String operator;

	private final String literalOperator;

	OperatorOperand(String literalOperator, String operator, String value) {
		this.literalOperator = literalOperator;
		this.value = value;
		this.operator = operator;
	}

	@Override
	public String sqlOf(CommonField key) {
		ColumnType columnType = ColumnType.from(key.structure().getType());
		String valueRepresentedAsSql = columnType.representAsSql(value);
		return Sql.of(key.name() + " " + operator + " " + valueRepresentedAsSql).toText();
	}

	@Override
	public JSONObject toJSON() {
		JSONObject result = new JSONObject();
		result.put("operator", literalOperator);
		result.put("value", value);
		return result;
	}

	public String getValue() {
		return value;
	}

	public String getOperator() {
		return operator;
	}
}
